package com.baizhi.service.impl;

import com.baizhi.entity.PmsAttrGroup;
import com.baizhi.entity.PmsCategory;
import com.baizhi.mapper.PmsCategoryMapper;
import com.baizhi.service.PmsCategoryService;
import com.baizhi.vo.CategoryListVo;
import com.baizhi.vo.CategoryVo;
import com.baizhi.vo.CategoryZTreeVo;
import com.baizhi.vo.PmsAttrGroupVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author yu
 */
@Service
public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryMapper, PmsCategory> implements PmsCategoryService {
    @Autowired
    private PmsCategoryMapper pmsCategoryMapper;

    @Override
    public List<CategoryVo> showCategoryList() {
        /*List<CategoryVo> categoryVos = pmsCategoryMapper.showCategoryList();
        return categoryVos;*/
        List<PmsCategory> pmsCategories = pmsCategoryMapper.selectList(null);
        List<CategoryVo> categoryVos = showTree((long) 0, pmsCategories);
        return categoryVos;
    }
    @Cacheable(cacheNames = "pmsCategories",key = "#root.methodName")
    @Override
    public List<CategoryZTreeVo> showCategoryZTreeList() {
        List<PmsCategory> pmsCategories = pmsCategoryMapper.selectList(null);
        List<CategoryZTreeVo> categoryZTreeVos = showZTree((long) 0, pmsCategories);
        return  categoryZTreeVos;
    }

    @Override
    public List<CategoryListVo> showCategoryListVos() {
        List<PmsCategory> pmsCategories = pmsCategoryMapper.selectList(null);
        List<CategoryListVo> categoryListVos = showListVos((long) 0, pmsCategories);
        return categoryListVos;
    }


    //    递归用流转换对象，进行Tree型结构构造
    private List<CategoryVo> showTree(Long parentId,List<PmsCategory> pmsCategories){
        List<CategoryVo> collect = pmsCategories.stream().filter(new Predicate<PmsCategory>() {
            @Override
            public boolean test(PmsCategory pmsCategory) {
                return pmsCategory.getParentCid() == parentId;
            }
        }).map(new Function<PmsCategory, CategoryVo>() {
            @Override
            public CategoryVo apply(PmsCategory pmsCategory) {
                CategoryVo categoryVo = new CategoryVo();
                categoryVo.setId(pmsCategory.getCatId());
                categoryVo.setTitle(pmsCategory.getName());
                categoryVo.setChildren(showTree(pmsCategory.getCatId(), pmsCategories));
                return categoryVo;
            }
        }).collect(Collectors.toList());
        return collect;
    }


    private List<CategoryZTreeVo> showZTree(Long parentId,List<PmsCategory> categories){
        List<CategoryZTreeVo> collect = categories.stream().filter(new Predicate<PmsCategory>() {
            @Override
            public boolean test(PmsCategory pmsCategory) {
                return pmsCategory.getParentCid() == parentId;
            }
        }).map(new Function<PmsCategory, CategoryZTreeVo>() {
            @Override
            public CategoryZTreeVo apply(PmsCategory pmsCategory) {
                CategoryZTreeVo zTreeVo = new CategoryZTreeVo();
                zTreeVo.setId(pmsCategory.getCatId());
                zTreeVo.setPid(pmsCategory.getParentCid());
                zTreeVo.setOpen(false);
                zTreeVo.setName(pmsCategory.getName());
                zTreeVo.setChildren(showZTree(pmsCategory.getCatId(), categories));
                return zTreeVo;
            }
        }).collect(Collectors.toList());
        return collect;
    }

    private List<CategoryListVo> showListVos(Long parentId,List<PmsCategory> pmsCategories){
        List<CategoryListVo> collect = pmsCategories.stream().filter(new Predicate<PmsCategory>() {
            @Override
            public boolean test(PmsCategory pmsCategory) {
                return pmsCategory.getParentCid() == parentId;
            }
        }).map(new Function<PmsCategory, CategoryListVo>() {
            @Override
            public CategoryListVo apply(PmsCategory pmsCategory) {
                CategoryListVo categoryListVo = new CategoryListVo();
                categoryListVo.setValue(pmsCategory.getCatId());
                categoryListVo.setLabel(pmsCategory.getName());
                categoryListVo.setChildren(showListVos(pmsCategory.getCatId(), pmsCategories));
                return categoryListVo;
            }
        }).collect(Collectors.toList());
        return collect;
    }
}
